#include "../inc/main.h"
#include "../inc/TCP.h"


TCP tcp_init(void)
{
    char port[15];
    printf("请输入端口:");
    scanf("%s" , port);
    // 创建套接字
    int server_sockfd;

    server_sockfd = socket( AF_INET , SOCK_STREAM , 0);

    if (server_sockfd == -1 )
    {
        ERRLOG("socket");
        exit(-1);
    }
    
    // 填写服务器网络信息
    struct sockaddr_in Server_addr;
    Server_addr.sin_family = AF_INET;
    Server_addr.sin_port = htons(atoi(port));
    // Server_addr.sin_addr.s_addr = inet_addr("106.53.217.128");
    Server_addr.sin_addr.s_addr = INADDR_ANY;

    socklen_t Server_len = sizeof(Server_addr);
    
    // 绑定
    if (0 != bind(server_sockfd , (struct sockaddr *)&Server_addr , Server_len))
    {
        ERRLOG("bind");
        exit(-1);
    }

    // 监听
    if (listen(server_sockfd, 3) < 0) {
        perror("listen failed");
        close(server_sockfd);
        exit(EXIT_FAILURE);
    }

    TCP tcp;
    tcp.Server_addr = Server_addr;
    tcp.server_sockfd = server_sockfd;

    return tcp;
}
